package com.useeinfo.oa.open.admin;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.AppVersionBiz;
import com.useeinfo.oa.modules.base.entity.AppVersion;
import com.useeinfo.oa.modules.security.enums.DeviceType;
import com.useeinfo.oa.modules.security.enums.Surroundings;
import com.useeinfo.oa.modules.wuHanCommon.base.thread.SendMailSecheduled;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/web/admin/appVersion")
public class AppVersionAction {

    @Autowired
    private AppVersionBiz appVersionBiz;
    @Autowired
    SendMailSecheduled sendMailSecheduled;

    /**
     * 打开列表页面
     */
    @RequestMapping("/getAppVersionListPage")
    public ModelAndView getAppVersionListPage() {

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("pages/admin/appVersion/appVersionList");
        //查询设备类型枚举类
        JSONArray deviceTypeArray = new JSONArray();
        for(DeviceType deviceType : DeviceType.values()){
            JSONObject json = new JSONObject();
            json.put("name",deviceType.getName());
            json.put("code",deviceType.getCode());
            deviceTypeArray.add(json);
        }
        modelAndView.addObject("deviceTypeArray", deviceTypeArray);
        //查询环境枚举类
        JSONArray surroundingsArray = new JSONArray();
        for(Surroundings surroundings : Surroundings.values()){
            JSONObject json = new JSONObject();
            json.put("name",surroundings.getName());
            json.put("code",surroundings.getCode());
            surroundingsArray.add(json);
        }
        modelAndView.addObject("surroundingsArray", surroundingsArray);
        return modelAndView;
    }

    /**
     * 分页获取JSON数据
     */
    @RequestMapping("/getAppVersionListJSON")
    @ResponseBody
    public JSONObject getAppVersionListJSON(@RequestParam(value = "pageNo", required = false) String pageNowParam,
                                            @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                            @RequestParam(value = "orderBy", required = false) String orderBy,
                                            @RequestParam(value = "searchCode", required = false) String searchCode,
                                            @RequestParam(value = "deviceType", required = false) String deviceType,
                                            @RequestParam(value = "surroundings", required = false) String surroundings) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);
        if(StringUtils.isNotBlank(searchCode)){
            queryParam.getSqlMap().put("searchCode", searchCode);
        }
        if(StringUtils.isNotBlank(deviceType)){
            queryParam.getSqlMap().put("deviceType", deviceType);
        }
        if(StringUtils.isNotBlank(surroundings)){
            queryParam.getSqlMap().put("surroundings", surroundings);
        }
        if (StringUtils.isNotBlank(orderBy))
            queryParam.setSqlOrder("order by " + orderBy);
        return appVersionBiz.findJSONList(queryParam);
    }

    /**
     * 获取编辑页面
     */
    @RequestMapping("/getAppVersionEditPage")
    public ModelAndView getAppVersionEditPage(@RequestParam(value = "id", required = false) String id) {


        AppVersion appVersion = appVersionBiz.findModel(StringConverters.ToLong(id));
        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("pages/admin/appVersion/appVersionEditPart");
        modelAndView.addObject("appVersion", appVersion);
        return modelAndView;
    }

    /**
     * 执行提交的新增或修改请求
     */
    @RequestMapping("/executeAppVersionEdit")
    @ResponseBody
    public ResultDto executeAppVersionEdit(AppVersion appVersion,
                                           @RequestParam(value = "select_deviceType[]", required = false) List<String> deviceTypes,
                                           @RequestParam(value = "select_surroundings[]", required = false) List<String> surroundingses) {
        if (CollectionUtils.isNotEmpty(deviceTypes))
            appVersion.setDeviceType(deviceTypes.get(0));
        if (CollectionUtils.isNotEmpty(surroundingses))
            appVersion.setSurroundings(surroundingses.get(0));
        if (appVersionBiz.exist(appVersion)) {
            return ResultUtil.error("已经存在，请重新输入！");
        }
        appVersionBiz.addOrUpdate(appVersion);
        return ResultUtil.success();
    }

    /**
     * 逻辑删除机构
     */
    @RequestMapping("/logicRemoveAppVersion")
    @ResponseBody
    public ResultDto logicRemoveAppVersion(@RequestParam(value = "id", required = false) String id) {
        appVersionBiz.deleteLogic(appVersionBiz.findModel(StringConverters.ToLong(id)));
        return ResultUtil.success();
    }
}
